package org.zjvis.datascience.web.api;

import cn.weiguangfu.swagger2.plus.annotation.ApiPlus;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.zjvis.datascience.common.dto.TaskInstanceDTO;
import org.zjvis.datascience.common.model.ApiResult;
import org.zjvis.datascience.common.model.ApiResultCode;
import org.zjvis.datascience.service.TaskInstanceService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@ApiPlus(value = true)
@RestController
@RequestMapping("/taskInstanceApi/")
@Api(tags = "taskInstanceApi", description = "taskInstance插件接口")
@Validated
public class TaskInstanceApi {
    @Autowired
    private TaskInstanceService taskInstanceService;

    @PostMapping(value = "/queryById")
    @Transactional
    @ResponseBody
    @ApiOperation(value = "queryById", notes = "queryById")
    public ApiResult<TaskInstanceDTO> queryById(@RequestBody JSONObject params) {
        Long id = params.getLong("id");
        return ApiResult.valueOf(taskInstanceService.queryById(id));
    }

    @PostMapping(value = "/queryActionByParentIdAndOrder")
    @Transactional
    @ResponseBody
    @ApiOperation(value = "queryActionByParentIdAndOrder", notes = "queryActionByParentIdAndOrder")
    public ApiResult<List<TaskInstanceDTO>> queryActionByParentIdAndOrder(@RequestBody JSONObject params) {
        String cid = params.getString("cid");
        return ApiResult.valueOf(taskInstanceService.queryActionByParentIdAndOrder(cid));
    }

    @PostMapping(value = "/deleteActionsByParentIdById")
    @Transactional
    @ResponseBody
    @ApiOperation(value = "deleteActionsByParentIdById", notes = "deleteActionsByParentIdById")
    public ApiResult<Void> deleteActionsByParentIdById(@RequestBody JSONObject params) {
        Map<String, Object> map = new HashMap<>();
        map.put("parentId", params.getString("parentId"));
        map.put("id", params.getLong("id"));
        taskInstanceService.deleteActionsByParentIdById(map);
        return ApiResult.valueOf(ApiResultCode.SUCCESS);
    }
}
